<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册 - 每日吸猫</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Arial', sans-serif;
            background: linear-gradient(135deg, #ff69b4, #9370db);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        
        h1 {
            color: white;
            margin-bottom: 30px;
            font-size: 2.5rem;
            font-weight: bold;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
            animation: float 3s ease-in-out infinite;
        }
        
        @keyframes float {
            0% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
            100% { transform: translateY(0px); }
        }
        
        .register-form {
            background: white;
            padding: 40px;
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
            text-align: center;
            position: relative;
            overflow: hidden;
        }
        
        .register-form::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, #ff69b4, #9370db);
        }
        
        h3 {
            color: #555;
            margin-bottom: 25px;
            font-size: 1.8rem;
            position: relative;
        }
        
        h3::after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 50%;
            transform: translateX(-50%);
            width: 60px;
            height: 3px;
            background: linear-gradient(90deg, #ff69b4, #9370db);
            border-radius: 2px;
        }
        
        input {
            width: 100%;
            padding: 15px;
            margin: 10px 0;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
            background: #f9f9f9;
        }
        
        input:focus {
            outline: none;
            border-color: #ff69b4;
            background: white;
            box-shadow: 0 0 10px rgba(255, 105, 180, 0.3);
        }
        
        .captcha-container {
            display: flex;
            gap: 10px;
            margin: 10px 0;
        }
        
        .captcha-input {
            flex: 1;
        }
        
        .captcha-display {
            flex: 0 0 100px;
            background: #f0f0f0;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 18px;
            font-weight: bold;
            color: #333;
            cursor: pointer;
            user-select: none;
        }
        
        .captcha-display:hover {
            background: #e8e8e8;
        }
        
        button {
            width: 100%;
            padding: 15px;
            margin: 10px 0;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }
        
        .register-button {
            background: linear-gradient(135deg, #ff69b4, #9370db);
            color: white;
        }
        
        .back-button {
            background: #6c757d;
            color: white;
        }
        
        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
        }
        
        button:active {
            transform: translateY(0);
        }
        
        .error-message {
            color: #ff6347;
            font-size: 14px;
            margin-top: 15px;
            padding: 10px;
            background: rgba(255, 99, 71, 0.1);
            border-radius: 10px;
            border-left: 4px solid #ff6347;
            animation: shake 0.5s ease;
        }
        
        .success-message {
            color: #28a745;
            font-size: 14px;
            margin-top: 15px;
            padding: 10px;
            background: rgba(40, 167, 69, 0.1);
            border-radius: 10px;
            border-left: 4px solid #28a745;
        }
        
        .hidden {
            display: none;
        }
        
        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
            20%, 40%, 60%, 80% { transform: translateX(5px); }
        }
        
        @media (max-width: 768px) {
            .register-form {
                padding: 30px 20px;
                margin: 20px;
                max-width: 100%;
            }
            
            h1 {
                font-size: 2rem;
                margin-bottom: 20px;
            }
            
            h3 {
                font-size: 1.5rem;
            }
        }
    </style>
</head>
<body>
    <h1>😻 注册新账号 😻</h1>
    <div class="register-form">
        <h3>创建账号</h3>
        <input type="text" id="username" placeholder="用户名">
        <input type="password" id="password" placeholder="密码">
        <input type="password" id="confirm-password" placeholder="确认密码">
        <div class="captcha-container">
            <input type="text" id="captcha" class="captcha-input" placeholder="验证码">
            <div id="captcha-display" class="captcha-display">1234</div>
        </div>
        <button id="register-button" class="register-button">注册</button>
        <button id="back-button" class="back-button">返回登录</button>
        <div id="error-message" class="error-message hidden"></div>
        <div id="success-message" class="success-message hidden"></div>
    </div>

    <script>
        // 获取DOM元素
        const usernameInput = document.getElementById('username');
        const passwordInput = document.getElementById('password');
        const confirmPasswordInput = document.getElementById('confirm-password');
        const captchaInput = document.getElementById('captcha');
        const captchaDisplay = document.getElementById('captcha-display');
        const registerButton = document.getElementById('register-button');
        const backButton = document.getElementById('back-button');
        const errorMessage = document.getElementById('error-message');
        const successMessage = document.getElementById('success-message');
        
        let currentCaptcha = '1234';
        
        // 生成验证码
        function generateCaptcha() {
            currentCaptcha = String(Math.floor(1000 + Math.random() * 9000));
            captchaDisplay.textContent = currentCaptcha;
        }
        
        // 初始化验证码
        generateCaptcha();
        
        // 点击验证码刷新
        captchaDisplay.addEventListener('click', generateCaptcha);
        
        // 注册功能
        async function register() {
            const username = usernameInput.value;
            const password = passwordInput.value;
            const confirmPassword = confirmPasswordInput.value;
            const captcha = captchaInput.value;
            
            // 验证输入
            if (!username || !password || !confirmPassword || !captcha) {
                showError('请填写所有字段');
                return;
            }
            
            if (password !== confirmPassword) {
                showError('密码和确认密码不一致');
                return;
            }
            
            if (password.length < 6) {
                showError('密码长度至少6位');
                return;
            }
            
            if (captcha !== currentCaptcha) {
                showError('验证码错误');
                generateCaptcha();
                return;
            }
            
            try {
                // 发送注册请求
                const response = await fetch('http://localhost:8080/api/user/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password,
                        confirmPassword: confirmPassword,
                        captcha: captcha
                    })
                });
                
                const data = await response.json();
                
                if (data.success) {
                    showSuccess('注册成功！正在跳转到登录页面...');
                    setTimeout(() => {
                        window.location.href = 'index.html';
                    }, 2000);
                } else {
                    showError(data.message || '注册失败');
                    generateCaptcha();
                }
            } catch (error) {
                console.error('注册失败:', error);
                showError('注册失败，请检查网络连接或联系管理员');
                generateCaptcha();
            }
        }
        
        // 显示错误信息
        function showError(message) {
            errorMessage.textContent = message;
            errorMessage.classList.remove('hidden');
            successMessage.classList.add('hidden');
            setTimeout(() => {
                errorMessage.classList.add('hidden');
            }, 5000);
        }
        
        // 显示成功信息
        function showSuccess(message) {
            successMessage.textContent = message;
            successMessage.classList.remove('hidden');
            errorMessage.classList.add('hidden');
        }
        
        // 返回登录页面
        function goBack() {
            window.location.href = 'index.html';
        }
        
        // 事件监听
        registerButton.addEventListener('click', register);
        backButton.addEventListener('click', goBack);
        
        // 回车键注册
        usernameInput.addEventListener('keyup', (e) => {
            if (e.key === 'Enter') {
                register();
            }
        });
        
        passwordInput.addEventListener('keyup', (e) => {
            if (e.key === 'Enter') {
                register();
            }
        });
        
        confirmPasswordInput.addEventListener('keyup', (e) => {
            if (e.key === 'Enter') {
                register();
            }
        });
        
        captchaInput.addEventListener('keyup', (e) => {
            if (e.key === 'Enter') {
                register();
            }
        });
    </script>
</body>
</html>